package cn.codesugar.data.storage

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import cn.codesugar.data.storage.databinding.ActivitySharedPreferencesTestBinding

class SharedPreferencesTestActivity : AppCompatActivity() {

    private var b: ActivitySharedPreferencesTestBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivitySharedPreferencesTestBinding.inflate(layoutInflater)
        setContentView(b?.root)
        b?.saveButton?.setOnClickListener {
            val editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
            editor.putString("name", "CodeSugar")
            editor.putInt("age", 25)
            editor.putBoolean("married", false)
            editor.putFloat("IQ", 159.955F)
            editor.putLong("timeStamp", System.currentTimeMillis())
            editor.putStringSet("talent", setOf("Java", "Kotlin", "Python", "C#", "Dart", "Html", "CSS", "JavaScript"))
            editor.apply()
        }
        b?.restoreButton?.setOnClickListener {
            val sp = getSharedPreferences("data", Context.MODE_PRIVATE)
            val name = sp.getString("name", "")
            val age = sp.getInt("age", 0)
            val married = sp.getBoolean("married", false)
            val iq = sp.getFloat("IQ", 0.0F)
            val timeStamp = sp.getLong("timeStamp", 0)
            val talent = sp.getStringSet("talent", setOf(""))
            Log.e("20210610", " \nname:$name\nage:$age\nmarried:$married\niq:$iq\ntimeStamp:$timeStamp\ntalent:$talent")
        }
    }
}